package com.lzx.api;


/**
 * 通用返回对象
 * @param <T>
 */
public class CommonResult<T> {
    private long code;
    private String msg;
    private T data;

    protected CommonResult(){}

    protected CommonResult(long code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回结果
     * @param data  获取的数据
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T data){
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(),
                com.lzx.api.ResultCode.SUCCESS.getMsg(),data);
    }

    /**
     * 成功返回状态码和信息
     * @param code
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(long code,String msg){
        return new CommonResult<T>(code,msg,null);
    }


    /**
     * 成功返回结果和信息
     * @param data
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T data, String msg)
    {
        return new CommonResult<T>(com.lzx.api.ResultCode.SUCCESS.getCode(), msg, data);

    }

    /**
     * 失败返回状态码
     * @param code
     * @param <T>
     * @return
     */
    public static  <T> CommonResult<T> failed(IErrorCode code){
        return new  CommonResult<T> (code.getCode(),null, null);
    }

    /**
     * 失败返回信息
     * @param message
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failed(String message){
        return new CommonResult<T>(com.lzx.api.ResultCode.FAILED.getCode(),message,null);
    }

    /**
     * 失败返回结果
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> failed(){
        return failed(ResultCode.FAILED);
    }

    /**
     * 校验失败返回结果
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> validateFailed(){
        return failed(com.lzx.api.ResultCode.VALIDATE_FAILED);
    }

    /**
     * 校验失败返回信息
     * @param message
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> validateFailed(String message){
        return new CommonResult<T>(com.lzx.api.ResultCode.VALIDATE_FAILED.getCode(),message,null);
    }

    /**
     * 未登录返回结果
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> unauthorized(T data){
        return new CommonResult<T>(com.lzx.api.ResultCode.UNAUTHORIZED.getCode(), com.lzx.api.ResultCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 未授权返回结果
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> forbidden(T data){
        return new CommonResult<T>(com.lzx.api.ResultCode.FORBIDDEN.getCode(), com.lzx.api.ResultCode.FORBIDDEN.getMsg(), data);
    }


    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

